package com.briup.bookstore.service.impl;

import com.briup.bookstore.dto.UserLoginDTO;
import com.briup.bookstore.dto.UserMessageUpdateDTO;
import com.briup.bookstore.dto.UserRegisterDTO;
import com.briup.bookstore.dto.UserStatusUpdateDTO;
import com.briup.bookstore.exception.BookStoreException;
import com.briup.bookstore.mapper.UserMapper;
import com.briup.bookstore.po.User;
import com.briup.bookstore.service.UserService;
import com.briup.bookstore.utils.BeanCopyUtils;
import com.briup.bookstore.utils.JsonWebTokenUtils;
import com.briup.bookstore.vo.UserInfoVO;
import com.briup.bookstore.vo.UserLoginVO;
import com.briup.bookstore.vo.UserPageVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserLoginVO login(UserLoginDTO adminLoginDTO, Integer roleId) {
        String username = adminLoginDTO.getUsername();
        // 判断用户名是否为空
        if(!StringUtils.hasText(username)){
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_USERNAME_IS_NOT_NULL);
        }
        String password = adminLoginDTO.getPassword();
        // 判断密码是否为空
        if(!StringUtils.hasText(password)){
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_PASSWORD_IS_NOT_NULL);
        }

        User user = userMapper.selectUserByUsernameAndPassword(adminLoginDTO);
        // 判断user是否为空
        if(Objects.isNull(user)){
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_USERNAME_OR_PASSWORD_ERROR);
        }

        // 判断账号的状态是否可用
        if(user.getStatus() == 1){
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_STATUS_CLOSE);
        }

        // 原则上需要判断用户的角色是否是管理员
        if(user.getRoleId() != roleId){
            throw new BookStoreException(BookStoreException.CodeMsgEnum.LOGIN_IDENTITY_IS_INVALID);
        }

        // 如果所有验证都通过，根据user的id，生成一个jwt的字符串
        Integer id = user.getId();
        String jwt = JsonWebTokenUtils.createJWT(String.valueOf(id));

        System.out.println("user = " + user);

        // 复制一份user
        UserInfoVO userInfoVO = BeanCopyUtils.copyBean(user, UserInfoVO.class);

        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setUserInfo(userInfoVO);
        userLoginVO.setJwt(jwt);

        return userLoginVO;
    }

    @Override
    public PageInfo<UserPageVO> getUserPage(Integer pageNum, Integer pageSize, String username, String status, String startTime, String endTime) {
        return null;
    }

    @Override
    public void register(UserRegisterDTO userRegisterDTO) {

    }

    @Override
    public void updateUserStatus(UserStatusUpdateDTO userStatusUpdateDTO) {

    }

    @Override
    public void deleteUser(String ids) {

    }

    @Override
    public UserInfoVO getUserInfo(String id) {
        User user = userMapper.selectById(id);
        UserInfoVO userInfoVO = BeanCopyUtils.copyBean(user, UserInfoVO.class);
        return userInfoVO;
    }

    @Override
    public UserInfoVO updateUserMessage(UserMessageUpdateDTO userMessageUpdateDTO) {
        return null;
    }

    @Override
    public void updatePassword(int parseInt, String oldPassword, String newPassword) {

    }
}
